package cj.web.marriage.controllers;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.domain.UserInfo;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.MarriageOutputTypeEnum;
import cj.web.marriage.constants.MarriageRegStatusEnum;
import cj.web.marriage.constants.MarriageRegTypeEnum;
import cj.web.marriage.constants.RegAddressEnum;
import cj.web.marriage.dao.MarriageInforDAO;
import cj.web.marriage.dao.RefAddressAuthoDAO;
import cj.web.marriage.domain.MarriageInfor;

@Path("marriageexport")
public class MarriageExportController extends BaseController{
	
	private static Logger logger = Logger.getLogger(MarriageInforController.class.getName());

	@Autowired
	private MarriageInforDAO marriageInforDAO;
	
	@Autowired
	private RefAddressAuthoDAO refAddressAuthoDAO;
	
	@Get("show")
	public String show(Model model){
		model.add("MemuEnum",MemuEnum.marriageexport);
        EnumSet<MarriageOutputTypeEnum> marriageOutputTypeEnum = EnumSet.allOf(MarriageOutputTypeEnum.class);
        model.add("OutputEnum",marriageOutputTypeEnum);
        return "marriage/marriageOutputList.vm";
	}
	
	@Get("output")  
	public void output(@Param("start")String start,@Param("end")String end, @Param("type")int type, 
			 HttpServletRequest request, HttpServletResponse response) throws Exception {  
		 String sheetName = MarriageOutputTypeEnum.getName(type);
		 String title = URLEncoder.encode(sheetName, "UTF-8");  // 进行转码，使其支持中文文件名  
	     response.setContentType("application/vnd.ms-excel");  // 生成提示信息 
	     response.setHeader("content-disposition", "attachment;filename=" + title + ".xls");  
	     OutputStream fOut = response.getOutputStream();
	     
	     List<Map> dataset = null;
	     ExportExcel<MarriageInfor> ex = new ExportExcel<MarriageInfor>();
	     
		 //登录用户ID
		 int userId = returnSessionObj(request).getId();
		 //查询地址权限
		 List<Integer> addressIdlis = refAddressAuthoDAO.queryByUserId(userId);
	     
	     if(type == MarriageOutputTypeEnum.DBKQY.getValue()){
	    	 //结婚预约信息
	    	 String[] headers = { "序号","幸福码","男方","身份证","女方","身份证","预约类型","预约时间","地址","联系电话","状态","创建时间" };
	         String[] fields = {"id","code","man","manCard","woman","womanCard","type","regTime","addressId","link","status","createAt"};
	         dataset = marriageInforDAO.marriageInforExcel(start,end,addressIdlis);
	         for(Map map : dataset){
	        	 map.put("addressId",RegAddressEnum.getName(Integer.valueOf(map.get("addressId")!=null?map.get("addressId").toString():"-1")));
	        	 map.put("type",MarriageRegTypeEnum.getName(Integer.valueOf(map.get("type")!=null?map.get("type").toString():"-1")));
	        	 map.put("status",MarriageRegStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }else if(type == MarriageOutputTypeEnum.ZXDB.getValue()){
	    	//信息提交
	    	 String[] headers = { "序号","幸福码","男方","电话","女方","电话","创建时间","地址" };
	         String[] fields = {"id","code","man","manPhone","woman","womanPhone","createAt","addressId"};
	         dataset = marriageInforDAO.marriageTJExcel(start,end);
	         for(Map map : dataset){
	        	 map.put("addressId",RegAddressEnum.getNameOnly(Integer.valueOf(map.get("addressId")!=null?map.get("addressId").toString():"-1")));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
	     
		 fOut.flush();
		 fOut.close();
	 }  
}
